﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using MWTools.Client.Common.Interfaces;

namespace MWTools.Client.UI
{
    public partial class FrmOptions : Form
    {

        public string UserName
        {
            get { return txtUserName.Text; }
            set
            {
                txtUserName.Text = value;
            }
        }

        public string Password
        {
            get { return txtPassword.Text; }
            set
            {
                txtPassword.Text = value;
            }
        }

        public string WikiUri
        {
            get { return txtWikiUri.Text; }
            set
            {
                txtWikiUri.Text = value;
            }
        }
        


        public FrmOptions()
        {
            InitializeComponent();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            AppContext.Current.UserSetting.MWToolsServerUri = WikiUri;
            AppContext.Current.UserSetting.UserName = UserName;
            AppContext.Current.UserSetting.Password = Password;
            AppContext.Current.UserSetting.CurrentPlugin = lbPlugins.Items[lbPlugins.SelectedIndex].ToString();
            AppContext.Current.Save();
        }

        private void FrmOptions_Load(object sender, EventArgs e)
        {
            bool settingSelected = false;
            txtUserName.Text = AppContext.Current.UserSetting.UserName;
            txtPassword.Text = AppContext.Current.UserSetting.Password;
            txtWikiUri.Text = AppContext.Current.UserSetting.MWToolsServerUri;

            System.IO.FileInfo fileInfo = new System.IO.FileInfo(Assembly.GetExecutingAssembly().Location);

            System.IO.DirectoryInfo pluginDirectory = fileInfo.Directory;

            foreach (var item in pluginDirectory.GetFiles("*Plugins.dll"))
            {
                Assembly asm = Assembly.LoadFrom(item.FullName);
                foreach (Type type in asm.GetTypes())
                {
                    if (type.BaseType != null && type.BaseType.Name == "ContentSubmitterBase")
                    {
                        int index = lbPlugins.Items.Add(type.FullName);

                        //HACK
                        AppContext.Current.UserSetting.PluginAssembly = asm.Location;
                        if (AppContext.Current.UserSetting.CurrentPlugin == type.FullName)
                        {
                            AppContext.Current.UserSetting.PluginAssembly = asm.Location;
                            lbPlugins.SelectedIndex = index;
                            settingSelected = true;
                        }
                    }

                }
            } 

            if (!settingSelected && lbPlugins.Items.Count > 0)
                lbPlugins.SelectedIndex = 0;



        }

        private void grpWikiOptions_Enter(object sender, EventArgs e)
        {

        }

        private void tabPage3_Click(object sender, EventArgs e)
        {

        }

        private void tabPage2_Click(object sender, EventArgs e)
        {

        }

    }
}
